#ifndef __FLDRV_I2C_H
#define __FLDRV_I2C_H
#include "at32f425.h"

#define ENS210_I2C_WADDR 0x86	//I2C写地址：7位I2C地址+一位写
#define ENS210_I2C_RADDR 0x87	//I2C写地址：7位I2C地址+一位读

#define I2C_SCL_PIN               GPIO_PINS_10
#define I2C_SCL_GPIO              GPIOB
#define I2C_SDA_PIN               GPIO_PINS_11
#define I2C_SDA_GPIO              GPIOB
#define I2C_GPIO_CRM_CLK          CRM_GPIOB_PERIPH_CLOCK

#define SCL_PIN_H 								GPIOB->scr = GPIO_PINS_10
#define SCL_PIN_L 								GPIOB->clr = GPIO_PINS_10
#define SDA_PIN_H 								GPIOB->scr = GPIO_PINS_11
#define SDA_PIN_L 								GPIOB->clr = GPIO_PINS_11

#define SDA_SET_INPUT							GPIOB->cfgr  &= (uint32_t)~(0x03 << (GPIO_PINS_SOURCE11 * 2));\
																	GPIOB->cfgr  |= (uint32_t)(GPIO_MODE_INPUT << (GPIO_PINS_SOURCE11 * 2))
#define SDA_SET_OUTPUT						GPIOB->cfgr  &= (uint32_t)~(0x03 << (GPIO_PINS_SOURCE11 * 2));\
																	GPIOB->cfgr  |= (uint32_t)(GPIO_MODE_OUTPUT << (GPIO_PINS_SOURCE11 * 2))
																	
#define READ_SDA_DATA							gpio_input_data_bit_read(I2C_SDA_GPIO,I2C_SDA_PIN)


// Chip constants
#define ENS210_PARTID          0x0210 		// The expected part id of the ENS210
#define ENS210_BOOTING         2 		// Booting time in ms (also afloater reset, or going to high power)
#define ENS210_THCONV_SINGLE_MS 130 // Conversion time in ms for single shot T/H measurement
#define ENS210_THCONV_CONT_MS   238 // Conversion time in ms for continuous T/H measurement

// Addresses of the ENS210 registers
#define ENS210_REG_PART_ID_L     0x00
#define ENS210_REG_PART_ID_H     0x01
#define ENS210_REG_DIE_REV       0x02
#define ENS210_REG_UID           0x04
#define ENS210_REG_SYS_CTRL      0x10
#define ENS210_REG_SYS_STAT      0x11
#define ENS210_REG_SENS_RUN      0x21
#define ENS210_REG_SENS_START    0x22
#define ENS210_REG_SENS_STOP     0x23
#define ENS210_REG_SENS_STAT     0x24
#define ENS210_REG_T_L         	 0x30
#define ENS210_REG_T_M           0x31
#define ENS210_REG_T_H           0x32
#define ENS210_REG_H_L           0x33
#define ENS210_REG_H_M           0x34
#define ENS210_REG_H_H           0x35


void fldrv_i2c_init(void);
void fldrv_i2c_write_byte(unsigned char RAddr, unsigned char WData);
void fldrv_i2c_read_byte(unsigned char RAddr, unsigned char *RData);
void fldrv_i2c_read_data(unsigned char RAddr, unsigned int *temper,unsigned int *humi);
#endif
